package com.winning.pmph.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winning.pmph.entity.KnowledgeField;
import com.winning.pmph.entity.TempKnowledge;
import com.winning.pmph.entity.TempKnowledgeField;
import com.winning.pmph.mapper.TempKnowledgeMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
@Transactional
public class TempKnowledgeService extends ServiceImpl<TempKnowledgeMapper, TempKnowledge> {

    @Resource
    private TempKnowledgeFieldService tempKnowledgeFieldService;

    public void saveBeanAndField(TempKnowledge knowledge) {
        saveOrUpdate(knowledge);
        // 更新原字段;
        tempKnowledgeFieldService.remove(Wrappers.lambdaQuery(TempKnowledgeField.class)
                .eq(KnowledgeField::getKnowledgeId, knowledge.getId())
                .eq(TempKnowledgeField::getHidden, 0));
        tempKnowledgeFieldService.saveBatch(knowledge.getTempKnowledgeFieldList());
    }
}